package util
{
	import business.LogDelegate;
	
	import events.MyLogEvent;
	
	import flash.events.EventDispatcher;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	
	public class LogUtil extends EventDispatcher
	{
		private static var instance:LogUtil;
		public static const ADD:String = 'add';
		public static const DEL:String = 'del';
		public static const UPDATE:String = 'update';
		public static const PUBLISH:String = 'publish';
		public static const SAVE:String = 'save';
				
		public var logDelegate:LogDelegate;
		private var logEvent:MyLogEvent;
		public var logQue:Array; 		
		
		public function LogUtil()
		{
			logDelegate = new LogDelegate(this);
			logQue = new Array();
		}
		
		public static function getInstance() : LogUtil
		{
			if ( instance == null )
			{
				instance = new LogUtil();
			}	
			return instance;
	    }
	    
	    public function log(uid:String, username:String, mid:String, type:String, record:*='', targetId:String='', targetType:String='', setOpt:Boolean=false):void
	    {
    		var typeStr:String;
    		if(type == UPDATE)
    		{
    			typeStr = '更新:';
    		}
    		else if(type == DEL)
    		{
    			typeStr = '删除:';
    		}
    		else if(type == ADD)
    		{
    			typeStr = '添加:';
    		}
    		else if(type == PUBLISH)
    		{
    			typeStr = '发布:';
    		}
    		else if(type == SAVE)
    		{
    			typeStr = '保存:';
    		}
    			    	
	    	var obj:Object = new Object();
	    	obj.uid = uid;
	    	obj.username = username;
	    	obj.mid = mid;
	    	if(!(record is String))
	    	{
	    		obj.record = typeStr+adjustRecord(record);
	    	}
	    	else
	    	{
	    		obj.record = typeStr+record;
	    	}
	    	obj.targetId = targetId;
	    	obj.targetType = targetType;
	    	//obj.time = (new Date().getTime()) / 1000;
	    	if(setOpt)
	    	{
	    		obj.optSuccessd = '1';
	    	}
	    	else
	    	{
	    		obj.optSuccessd = '0';
	    	}
	    	logEvent = new MyLogEvent(record);
	    	logDelegate.log(obj);
	    }
	    
	    private function adjustRecord(record:Object):String
	    {
	    	var retRecord:String = '';
	    	for (var i:String in record)
	    	{
	    		if((record[i] is Number) || (record[i] is String) )
	    		{
	    			retRecord += i + '=' + record[i] + ',';
	    		}
				else if((record[i] is Array))
				{
					var arr:Array = record[i];
					
					retRecord +=  arr.toString() +",";
				}
	    		else
	    		{
	    			retRecord += adjustRecord(record[i]);
	    		}
	    	}
	    	if(retRecord.length > 0)
	    	{
	    		retRecord = retRecord.substr(0, retRecord.length - 1) + '.';
	    	}
	    	
	    	return retRecord;
	    }
	    
		public function logResult(result:Object, token:Object = null):void
		{
			result = result.result;
			if(result.code == 0)
			{
				this.logQue.push(result.id);
			}
			else
			{
				Alert.show(result.msg);
			}
			if(logEvent)
			{
				logEvent.logId = result.id;
				this.dispatchEvent(logEvent);
				logEvent = null;
			}
		}
		
		public function setOptSuccessd(id:String):void
		{
			logQue.splice(logQue.indexOf(id),1);	
			logDelegate.setOpt(id, 1);  //操作成功
		}
		
		public function setOptResult(result:Object, token:Object = null):void
		{
		}
		
		public function fault(error:FaultEvent,token:Object = null):void
		{
			Alert.show(error.fault.faultString,'error');
		}				

	}
}